<?php
/**
 * 手动替换订单时实例化
 */
class ome_order_manualreplace{

	//订单物料替换
	function orderReplace($order_idsarr,$item_list,$material,$material_replace,&$msg) {
			//判断替换物料(销售物料)
			$basicMaterialExtObj = app::get('material')->model('basic_material');
			$salesMaterialExtObj = app::get('material')->model('sales_material');
			$sales_basic_material= app::get('material')->model('sales_basic_material');
			$sales_material_info = $salesMaterialExtObj->dump(array('sales_material_bn'=>$material_replace), 'sm_id,sales_material_name,sales_material_bn');
			if(!$sales_material_info){
				$msg[] = '替换銷售物料'.$material_replace.'不存在';
            	return false;
			}
			//查询物料(基础物料绑定表)
			$basic_material_id = $sales_basic_material->dump(array('sm_id'=>$sales_material_info['sm_id']), 'bm_id');
			if(!$basic_material_id){
				$msg[] = '替换物料'.$material_replace.'未绑定基础物料';
            	return false;
			}
			$basic_material = $basicMaterialExtObj->dump(array('bm_id'=>$basic_material_id['bm_id']), 'material_bn,material_name');
			if(!$basic_material){
				$msg[] = '替换物料'.$material_replace.'绑定基础物料'.$basic_material['material_name']."不存在";
            	return false;
			}
			//执行替换
			if($item_list && $order_idsarr){
	            $omeOrderItemMdl   = app::get('ome')->model('order_items');
	            $omeOrderObjectMdl = app::get('ome')->model('order_objects');
	            $omeOrderMdl = app::get('ome')->model('orders');
			    $itemRepMdl = app::get('ome')->model('order_itemsRep');
			    $_opinfo = kernel::single('ome_func')->getDesktopUser();
	            $dataList = $omeOrderMdl->getList('order_id,order_bn,payed,pmt_order,total_amount', array('order_id' => $order_idsarr));
	            $objectdata = array();
	            $itemdata   = array();
	            foreach ($dataList as $key => $data) {
	                try{
	                	//查找原始物料数据是否为赠品
	                	$giftList = $omeOrderItemMdl->dump(array('order_id'=>$data['order_id'],'bn'=>$material,'item_type'=>'gift','delete|noequal'=>'true'));
	                	if($giftList){
	                    	$msg[] = '订单'.$data['order_bn'].'的物料'.$material.'为赠品,不能替换';
	                    	continue;
	                    }
	                    //查找原始物料数据
	                    $itemList = $omeOrderItemMdl->dump(array('order_id'=>$data['order_id'],'bn'=>$material,'item_type|in'=>array('product','pkg'),'delete|noequal'=>'true'));
	                    $objectList = $omeOrderObjectMdl->dump(array('order_id'=>$data['order_id'],'obj_id'=>$itemList['obj_id'],'delete|noequal'=>'true'));
	                    if(!$itemList || !$objectList){
	                    	$msg[] = '订单'.$data['order_bn'].'不存在物料'.$material.'';
	                    	continue;
	                    }
	                    //更新删除原始物料数据
	                        $updateData['delete']='true';
	                        $res=$omeOrderItemMdl->update($updateData,array('item_id'=>$itemList['item_id']));
	                        $ores=$omeOrderObjectMdl->update($updateData,array('obj_id'=>$itemList['obj_id']));
	                        //添加替代物料数据
	                        if($res && $ores){
	                        	//order_objects数据写入
	                        	$objectdata = $objectList;
	                        	unset($objectdata['obj_id']);
	                        	$objectdata['order_id'] = $data['order_id'];
	                        	$objectdata['goods_id'] = $sales_material_info['sm_id'];
	                        	$objectdata['bn'] = $sales_material_info['sales_material_bn'];
	                        	$objectdata['name'] = $sales_material_info['sales_material_name'];
	                        	$obj_result   = $omeOrderObjectMdl->insert($objectdata);
	                        	//order_items数据写入
	                        	$itemdata = $itemList;
	                        	unset($itemdata['item_id']);
	                        	$itemdata['obj_id']= $obj_result;
	                        	$itemdata['product_id']= $basic_material_id['bm_id'];
	                        	// $itemdata['bn']    = $sales_material_info['sales_material_bn'];
	                        	$itemdata['bn']    = $basic_material['material_bn'];
	                        	$itemdata['name']  = $basic_material['material_name'];
	                        	if(!$itemdata['nums'])
	                        	{
	                        		$itemdata['nums'] = $itemdata['quantity'];
	                        	};
	                        	$item_result   = $omeOrderItemMdl->insert($itemdata);
	                        	//为了统计报表用，添加替换明细
	                        	$itemList['newitem_id']=$item_result;
	                        	$itemList['nums']=$itemList['quantity'];
	                        	$itemList['op_time']=time();
	                        	$itemList['op_id']=$_opinfo['op_id'] ? $_opinfo['op_id'] : 16777215;
	                        	$itemList['op_name']=$_opinfo['op_name'] ? $_opinfo['op_name'] : "系统";
	                        	unset($itemList['quantity']);
			                    $tmp= $itemRepMdl->save($itemList);
	                        	//更改订单表中手动替换物料字段值,不让走自动替换物料
	                        	$omeOrderMdl->update(array('is_material_replace'=>'true','auto_status'=>'0'),array('order_id'=>$data['order_id']));
	                        	#记录操作日志
					            $oOperation_log = app::get('ome')->model('operation_log');
					            $oOperation_log->write_log('order_edit@ome',$data['order_id'],"订单物料".$material."手动替换为".$material_replace);
	                        }
	                }catch(Exception $e){
	                    $msg[] = '替换物料操作失败';
	            		return false;
	                }
	            }
	            return true;
	        }else{
	        	$msg[] = '订单数据不存在';
	            return false;
	        }

	}

}